const jwt = require('jsonwebtoken')
const config = require('../config')
const tools = {
  formatBody: (status, message, data) => {
    return {
      status,
      message: message ? message : (status === 1 ? '处理成功' : '处理失败'),
      data: data ? data : {}
    }
  },
  sleep: time => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve()
      }, time)
    })
  },
  parseToken: async token => {
    return new Promise(resolve => {
      let tokenObj
      try {
        tokenObj = jwt.verify(token, config.server.SIGN_KEY)
      } catch (err) {
        reject(err)
      }
      resolve(tokenObj)
    })
  }
}

module.exports = tools